package com.example.springboottestgit.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/12/14 9:05
 */

public class FileUtil {
    public static void createNewFile(String path) throws IOException {
        //String fileName = "test.txt";
        //File testFile = new File("E:" + File.separator + "GP" + File.separator + fileName);
        File testFile = new File(path);
        //返回的是File类型,可以调用exsit()等方法
        File fileParent = testFile.getParentFile();

        if (!fileParent.exists()) {
            fileParent.mkdirs();// 能创建多级目录
        }
        if (testFile.exists()) {
            testFile.delete();
            testFile.createNewFile();//有路径才能创建文件
        } else {
            testFile.createNewFile();//有路径才能创建文件
        }
    }

    public static void fileWriter(String path, String content) throws IOException {
        FileWriter fw = new FileWriter(path);
        fw.write(content);
        fw.close();
    }

    public static void txtWriter(String path, String content) {
        try {
            createNewFile(path);
            fileWriter(path,content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
